﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DTO;

namespace WindowsFormsApplication1
{
    public partial class CTHD : Form
    {
        public CTHD()
        {
            InitializeComponent();
        }
        public class sqlConnectionData
        {
            public static SqlConnection Ketnoi()
            {
                SqlConnection cnn = new SqlConnection(@"Data Source=SFFMB7SVKDP5HTX\SQLEXPRESS;Initial Catalog=Bakery;Integrated Security=True");
                return cnn;
            }
        }


        private void CTHD_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bakeryDataSet1.NHASX' table. You can move, or remove it, as needed.
             this.sANPHAMTableAdapter.Fill(this.bakeryDataSet2.SANPHAM);
            // TODO: This line of code loads data into the 'bakeryDataSet.LOAI' table. You can move, or remove it, as needed.
             this.hOADONTableAdapter.Fill(this.bakeryDataSet2.HOADON);
            dataGridView1.DataSource = CTHD_BUS.hienthicthd();
        }       
        private bool kiemtratrungdl()
        {
            bool tam = true;
            string ma = cbMaHD.Text;
            SqlConnection cnn = sqlConnectionData.Ketnoi();
            cnn.Open();
            string sql = "SELECT * from CTHD ";
            SqlCommand cmd = new SqlCommand(sql, cnn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (ma != dr.GetString(0))
                {
                    tam = false;
                    break;
                }
            }
            cnn.Close();
            return tam;
        }

        private void dataGridView1_RowSelected(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                cbMaHD.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim();
                cbMaBanh.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString().Trim();
                txtSoluong.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString().Trim();
                txtThanhtien.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString().Trim();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            {
                if (cbMaHD.Text == "" || cbMaBanh.Text == "" || txtSoluong.Text == "")
                {
                    MessageBox.Show("Vui lòng điền đầy đủ thông tin", "Thông Báo");
                    return;
                }

                if (kiemtratrungdl() == false)
                {
                    MessageBox.Show("Mã hóa đơn không đc khác", "Thông Báo");
                    return;
                }
                string mabanh;
                string mahd;


                SqlConnection cnn = sqlConnectionData.Ketnoi();
                cnn.Open();
                SqlCommand cmd = new SqlCommand("select * from SANPHAM where MaSP='" + cbMaBanh.SelectedValue.ToString() + "'", cnn);
                SqlDataReader r = cmd.ExecuteReader();


                r.Read();
                mabanh = r.GetString(0).ToString();
                r.Close();

                SqlCommand cmd1 = new SqlCommand("select * from HOADON where MaHD='" + cbMaHD.SelectedValue.ToString() + "'", cnn);
                SqlDataReader r1 = cmd1.ExecuteReader();

                r1.Read();
                mahd = r1.GetString(0).ToString();
                r1.Close();

                string thanhtien = txtThanhtien.Text;
                string soluong = txtSoluong.Text;

                CTHD_DTO cthd = new CTHD_DTO(mahd, mabanh, soluong, thanhtien);
                CTHD_BUS.ThemCTHD(cthd);
                dataGridView1.DataSource = SanPham_BUS.hienthi();
                cnn.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (cbMaHD.Text == "" || cbMaBanh.Text == "" || txtSoluong.Text == "")
            {
                MessageBox.Show("Vui lòng điền đầy đủ thông tin", "Thông Báo");
                return;
            }
            string macu = dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim();
            string mahd = cbMaHD.Text;
            if (macu != mahd)
            {
                MessageBox.Show("Không được sửa mã hóa đơn!", "Thông Báo");
                return;
            }
            string mabanh;

            SqlConnection cnn = sqlConnectionData.Ketnoi();
            cnn.Open();
            SqlCommand cmd = new SqlCommand("select * from SANPHAM where MaSP='" + cbMaBanh.SelectedValue.ToString() + "'", cnn);
            SqlDataReader r = cmd.ExecuteReader();


            r.Read();
            mabanh = r.GetString(0).ToString();
            r.Close();

            SqlCommand cmd1 = new SqlCommand("select * from HOADON where MaHD='" + cbMaHD.SelectedValue.ToString() + "'", cnn);
            SqlDataReader r1 = cmd1.ExecuteReader();

            r1.Read();
            mahd = r1.GetString(0).ToString();
            r1.Close();

            string thanhtien = txtThanhtien.Text;
            string soluong = txtSoluong.Text;

            CTHD_DTO cthd = new CTHD_DTO(mahd, mabanh, soluong, thanhtien);

            CTHD_BUS.SuaCTHD(cthd, mahd, macu);
            dataGridView1.DataSource = SanPham_BUS.hienthi();
            cnn.Close();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            string mahd = dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim();
            string mabanh = dataGridView1.CurrentRow.Cells[1].Value.ToString().Trim();
            string thanhtien = txtThanhtien.Text;
            string soluong = txtSoluong.Text;
            if (MessageBox.Show("Chắc chắn xóa chứ ?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                CTHD_DTO cthd = new CTHD_DTO(mahd, mabanh, soluong, thanhtien);
                CTHD_BUS.XoaCTHD(cthd, mahd, mabanh);
            }
            dataGridView1.DataSource = CTHD_BUS.hienthicthd();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.txtSoluong.Clear();
            this.txtTim.Clear();
            dataGridView1.DataSource = CTHD_BUS.hienthicthd();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mahd = txtTim.Text;
            CTHD_BUS.timkiem(mahd);
            dataGridView1.DataSource = CTHD_BUS.timkiem(mahd);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //TimkiemCTHD tk = new TimkiemCTHD();
            //tk.Show();
        }

    }


}